﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gadgets.Accountings
{
	public partial class eCashTransInquiry : WeifenLuo.WinFormsUI.Docking.DockContent  
	{
		public eCashTransInquiry()
		{
			InitializeComponent();
		}

		private void eCashTransInquiry_Load(object sender, EventArgs e)
		{
			try
			{
                ABSCommonLibrary.CreateGridView(ref gridControl1);
                DataSet tran_type = new DataSet();
                switch (this.Tag.ToString())
                {
                    case "00":
                        contextMenuStrip1.Enabled = false;
                        valueRow1.Visible = false;
                        tran_type = ABSCommonLibrary.DoInquiry(
                            CommonLibrary.CommonLibrary.BOSC014002, new object[3] { 1, 2, 0});
                        break;
                    case "12":
                        cmsBackDate.Visible = false;
                        cmsCancel.Visible = false;
                        valueRow1.Visible = false;
                        tran_type = ABSCommonLibrary.DoInquiry(
                            CommonLibrary.CommonLibrary.BOSC014002, new object[3] { 6, 0, 0 });
                        break;
                    case "01":
                        contextMenuStrip1.Enabled = false;
                        insertionRow1.Visible = false;
                        tran_type = ABSCommonLibrary.DoInquiry(
                            CommonLibrary.CommonLibrary.BOSC014002, new object[3] { 1, 0, 0 });
                        break;
                    case "02":
                        insertionRow1.Visible = false;
                        tran_type = ABSCommonLibrary.DoInquiry(
                            CommonLibrary.CommonLibrary.BOSC014002, new object[3] { 1, 0, 0 });
                        break;
                    case "22":
                        insertionRow1.Visible = false;
                        cmsBackDate.Visible = false;
                        cmsCancel.Visible = false;
                        tran_type = ABSCommonLibrary.DoInquiry(
                            CommonLibrary.CommonLibrary.BOSC014002, new object[3] { 4, 0, 0 });
                        break;
                    case "23":
                        insertionRow1.Visible = false;
                        contextMenuStrip1.Enabled = false;
                        tran_type = ABSCommonLibrary.DoInquiry(
                            CommonLibrary.CommonLibrary.BOSC014002, new object[3] { 4, 0, 0 });
                        break;
                    default:
                        break;
                }
                
                DataSet tran_type1 = tran_type.Copy();
                comboBoxEditor1.SetDataBinding(tran_type1, tran_type1.Tables[0].TableName);
                comboBoxEditor1.ValueMember = "CODE";
                comboBoxEditor1.DisplayFormat = "%DESCRIPTION%";                
                gridControl1.Columns["TRANS_TYPE"].CellEditorManager = comboBoxEditor1;

                comboBoxViewer1.SetDataBinding(tran_type, tran_type.Tables[0].TableName);
                comboBoxViewer1.ValueMember = "CODE";
                comboBoxViewer1.DisplayFormat = "%DESCRIPTION%";
                gridControl1.Columns["TRANS_TYPE"].CellViewerManager = comboBoxViewer1;

                colTRANS_DATE.FormatSpecifier = DeclareLibrary.DATE_DISPLAY_FORMAT;
                colAMOUNT.FormatSpecifier = DeclareLibrary.NUMBER_FORMAT;
                colDATE_CREATED.FormatSpecifier = DeclareLibrary.DATE_DISPLAY_FORMAT;

                
			}
			catch (Exception ex)
			{
				ABSCommonLibrary.ShowErrorMessage(ex.ToString());   
			}
		}
		private void load_grid()
		{
            DateTime trans_date = DeclareLibrary.MARKET_DAY;
            string account_no = string.Empty;            
            string status = string.Empty;
            int tran_type = 0;
            if (valueRow1.Cells["ACCOUNT_NO"].Value != null)
                account_no = valueRow1.Cells["ACCOUNT_NO"].Value.ToString();            
            if (valueRow1.Cells["TRANS_DATE"].Value != null)
                trans_date = Convert.ToDateTime(valueRow1.Cells["TRANS_DATE"].Value);
            if (this.Tag.ToString() == "22" || this.Tag.ToString() == "02")
                status = "P";
            else
                if (valueRow1.Cells["STATUS"].Value != null)
                    status = valueRow1.Cells["STATUS"].Value.ToString();            
            if (valueRow1.Cells["TRANS_TYPE"].Value != null)
                tran_type = Convert.ToInt16(valueRow1.Cells["TRANS_TYPE"].Value);
           
            DataSet v_data;
            if (this.Tag.ToString() == "22" || this.Tag.ToString() == "23") //Giao dich cua ngan hang
                v_data = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC014007, new object[4]{
                    trans_date, account_no, tran_type, status});
            else if (this.Tag.ToString() == "12") //Ung truoc tien ban CK
                v_data = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC014007, new object[4]{
                    trans_date, account_no, tran_type, status});
            else //Giao dich cua cong ty chung khoan
                v_data = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC014001, new object[4]{
                    trans_date, account_no, tran_type, status});
            gridControl1.SetDataBinding(v_data, v_data.Tables[0].TableName);
		}
		
        private void approve_cashtrans()
        {
            if (gridControl1.DataRows.Count == 0)
                return;
            string sQuestion = gridControl1.CurrentCell.ParentRow.Cells["TRANS_TYPE"].Value.ToString() +
                     " " + gridControl1.CurrentCell.ParentRow.Cells["AMOUNT"].Value.ToString() +
                     " cua tai khoan " + gridControl1.CurrentCell.ParentRow.Cells["ACCOUNT_NO"].Value.ToString();
            if (gridControl1.CurrentCell.ParentRow.Cells["STATUS"].Value.ToString() != "P")
            {
                ABSCommonLibrary.ShowMessage("Giao dich " + sQuestion + " khong duoc phep xac nhan 2 lan!");
                return;
            }

            if (ABSCommonLibrary.ShowQuestion("Ban muon xac nhan giao dich " + sQuestion + "?") == DialogResult.No)
                return;

            object[] p_param = new object[3];
            p_param[0] = gridControl1.CurrentCell.ParentRow.Cells["TRANS_ID"].Value;
            p_param[1] = DeclareLibrary.USER_NAME;
            p_param[2] = DeclareLibrary.MARKET_DAY;
            bool bSuccess = Convert.ToBoolean(ABSCommonLibrary.DoExecute(CommonLibrary.CommonLibrary.BOSC004004, ref p_param));

            if (bSuccess)
            {
                ABSCommonLibrary.ShowMessage("Xac nhan giao dich " + sQuestion + "thanh cong!");
            }
            else
            {
                ABSCommonLibrary.ShowMessage("Giao dich " + sQuestion + " khong thanh cong!");
            }
            load_grid();
        }
		private void cmsApprove_Click(object sender, EventArgs e)
		{
            try
            {
                approve_cashtrans();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
		}

		private void cancel_cashtrans()
		{
            if (gridControl1.DataRows.Count == 0)
                return;
            string sQuestion = gridControl1.CurrentCell.ParentRow.Cells["TRANS_TYPE"].Value.ToString() +
                     " " + gridControl1.CurrentCell.ParentRow.Cells["AMOUNT"].Value.ToString() +
                     " cua tai khoan " + gridControl1.CurrentCell.ParentRow.Cells["ACCOUNT_NO"].Value.ToString();
            if (gridControl1.CurrentCell.ParentRow.Cells["STATUS"].Value.ToString() != "C")
            {
                ABSCommonLibrary.ShowMessage("Khong duoc phep huy giao dich "
                    + sQuestion + " do giao dich nay chua duoc xac nhan!");
                return;
            }

			if (ABSCommonLibrary.ShowQuestion("Ban muon huy giao dich " + sQuestion + "?") == DialogResult.No)
				return;

			object[] p_param = new object[2];
            p_param[0] = gridControl1.CurrentCell.ParentRow.Cells["TRANS_ID"].Value;
			p_param[1] = DeclareLibrary.USER_NAME;			
			bool bSuccess = Convert.ToBoolean(ABSCommonLibrary.DoExecute(CommonLibrary.CommonLibrary.BOSC014012, ref p_param));
			if (bSuccess)
			{
				ABSCommonLibrary.ShowMessage("Huy giao dich " + sQuestion + "thanh cong!");
			}
			else
			{
				ABSCommonLibrary.ShowMessage("Giao dich " + sQuestion + " khong thanh cong!");
			}
			load_grid();
		}		

        private void adjust_cashtrans()
        {
            if (gridControl1.DataRows.Count == 0)
                return;
            string sQuestion = gridControl1.CurrentCell.ParentRow.Cells["TRANS_TYPE"].Value.ToString() +
                     " " + gridControl1.CurrentCell.ParentRow.Cells["AMOUNT"].Value.ToString() +
                     " cua tai khoan " + gridControl1.CurrentCell.ParentRow.Cells["ACCOUNT_NO"].Value.ToString();
            if (gridControl1.CurrentCell.ParentRow.Cells["STATUS"].Value.ToString() != "P")
            {
                ABSCommonLibrary.ShowMessage("Khong duoc phep hieu chinh giao dich "
                    + sQuestion + " do giao dich nay da duoc xac nhan!");
                return;
            }

            if (ABSCommonLibrary.ShowQuestion("Ban muon hieu chinh giao dich " + sQuestion + "?") == DialogResult.No)
                return;
            
            object[] p_param = new object[2];
            p_param[0] = gridControl1.CurrentCell.ParentRow.Cells["TRANS_ID"].Value;
            p_param[1] = DeclareLibrary.USER_NAME;
            bool bSuccess = Convert.ToBoolean(ABSCommonLibrary.DoExecute(CommonLibrary.CommonLibrary.BOSC014014, ref p_param));
            if (bSuccess)
            {
                ABSCommonLibrary.ShowMessage("Hieu chinh giao dich " + sQuestion + "thanh cong!");
            }
            else
            {
                ABSCommonLibrary.ShowMessage("Giao dich " + sQuestion + " khong thanh cong!");
            }
            load_grid();
        }

        private void cmsCancel_Click(object sender, EventArgs e)
        {
            try
            {
                cancel_cashtrans();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        private void cmsReject_Click(object sender, EventArgs e)
        {
            try
            {
                reject_cashtrans();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        private void cmsBackDate_Click(object sender, EventArgs e)
        {
            try
            {
                adjust_cashtrans();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
        private void reject_cashtrans()
        {
            if (gridControl1.DataRows.Count == 0)
                return;
            string sQuestion = gridControl1.CurrentCell.ParentRow.Cells["TRANS_TYPE"].Value.ToString() +
                     " " + gridControl1.CurrentCell.ParentRow.Cells["AMOUNT"].Value.ToString() +
                     " cua tai khoan " + gridControl1.CurrentCell.ParentRow.Cells["ACCOUNT_NO"].Value.ToString();
            if (ABSCommonLibrary.ShowQuestion("Ban muon xoa giao dich " + sQuestion + "?") == DialogResult.No)
                return;
            if (gridControl1.CurrentCell.ParentRow.Cells["STATUS"].Value.ToString() != "P")
            {
                ABSCommonLibrary.ShowMessage("Giao dich da duoc xac nhan, khong duoc phep xoa");
                return;
            }

            object[] p_param = new object[2];
            p_param[0] = gridControl1.CurrentCell.ParentRow.Cells["TRANS_ID"].Value;
            p_param[1] = DeclareLibrary.USER_NAME;

            bool bSuccess = Convert.ToBoolean(ABSCommonLibrary.DoExecute(CommonLibrary.CommonLibrary.BOSC014011, ref p_param));
            if (bSuccess)
            {
                ABSCommonLibrary.ShowMessage("Xoa giao dich " + sQuestion + "thanh cong!");
            }
            else
            {
                ABSCommonLibrary.ShowMessage("Giao dich " + sQuestion + " xoa khong thanh cong!");
            }
            load_grid();
        }

        private void eCashTransInquiry_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F9)
                {
                    if (((DataSet)gridControl1.DataSource).Tables[0].GetChanges() != null)
                    {
                        if (ABSCommonLibrary.ShowQuestion(
                            "Bo qua nhung thay doi giao dich vua duoc tao?") == DialogResult.No)
                            return;
                    }
                    load_grid();
                }
                else if (e.KeyCode == Keys.F11)
                {
                    if (((DataSet)gridControl1.DataSource).Tables[0].GetChanges() != null)
                    {
                        //CenturyFox.Service data_access = new CenturyFox.Service();
                        foreach (DataRow row in ((DataSet)gridControl1.DataSource).Tables[0].GetChanges().Rows)
                        {
                            if (row.RowState == DataRowState.Added)
                            {
                                object[] p_param = new object[8];
                                p_param[0] = row["TRANS_DATE"]; ;
                                p_param[1] = DeclareLibrary.MARKET_DAY;
                                p_param[2] = row["TRANS_TYPE"]; ;
                                p_param[3] = row["AMOUNT"];
                               
                                p_param[4] = row["ACCOUNT_NO"];
                                if (row["NOTES"] != DBNull.Value)
                                    p_param[5] = row["NOTES"];
                                else
                                    p_param[5] = string.Empty;
                                p_param[7] = DeclareLibrary.MARKET_DAY;
                                p_param[6] = DeclareLibrary.USER_NAME;
                                if (Convert.ToInt32(ABSCommonLibrary.DoExecute(
                                    CommonLibrary.CommonLibrary.BOSC014003, ref p_param)) == 0)
                                {
                                    ABSCommonLibrary.ShowMessage("Giao dich tien gui da duoc thuc hien");                                      
                                }
                                else
                                {
                                    ABSCommonLibrary.ShowMessage("Tai khoan nha dau tu khong ton tai");
                                }

                            }
                        }
                        load_grid();
                    }
                }
                else if (e.KeyCode == Keys.F10)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                    saveFileDialog.Filter = "Excel Files (*.xls)|*.xls";
                    saveFileDialog.AddExtension = true;
                    if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                    {
                        string FileName = saveFileDialog.FileName;
                        // TODO: Add code here to save the current contents of the form to a file.
                        Xceed.Grid.Exporting.ExcelExporter export = new Xceed.Grid.Exporting.ExcelExporter();
                        export.Export(gridControl1, FileName);
                    }
                }
                    
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
	}
}